package com.honey.mapreduce.flowcount;

import org.apache.hadoop.io.Writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

//自定义bean对象要想序列化传输，必须实现序列化接口
public class FlowBean implements Writable {

    private long max;
    private long min;
    private long sum;

    //反序列化时，需要反射调用空参构造函数，所以必须有空参构造
    public FlowBean(){
        super();
    }

    public FlowBean(long max,long min){
        this.max = max;
        this.min = min;
        this.sum = max + min;
    }

    /**
     * 序列化方法
     * @param dataOutput
     * @throws IOException
     */
    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeLong(max);
        dataOutput.writeLong(min);
        dataOutput.writeLong(sum);
    }

    /**
     * 反序列化方法
     * 注意反序列化的顺序和序列化的顺序完全一致
     * @param dataInput
     * @throws IOException
     */
    @Override
    public void readFields(DataInput dataInput) throws IOException {
        max = dataInput.readLong();
        min = dataInput.readLong();
        sum = dataInput.readLong();
    }

    public long getMax() {
        return max;
    }

    public void setMax(long max) {
        this.max = max;
    }

    public long getMin() {
        return min;
    }

    public void setMin(long min) {
        this.min = min;
    }

    public long getSum() {
        return sum;
    }

    public void setSum(long sum) {
        this.sum = sum;
    }

    //要想把结果显示在文件中，需要重写toString()，且用”\t”分开，方便后续用
    @Override
    public String toString() {
        return  max +"\t" + min + " \t" + sum + " \t";
    }
}

